package com.risetechnology.services.appservices;
import java.io.File;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.os.Vibrator;
import android.util.Log;
import android.widget.Toast;
/**
 * 
 * @author AN
 *
 */
public class CountDownService extends Service{
	/*
	 * field
	 */
	public static final String ACTION_CDT1_TICK="cdt1_tick";
	public static final String ACTION_CDT2_TICK="cdt2_tick";
	public static final String ACTION_CDT3_TICK="cdt3_tick";
	
	public static final String ACTION_CDT1_FINISH="cdt1_finish";
	public static final String ACTION_CDT2_FINISH="cdt2_finish";
	public static final String ACTION_CDT3_FINISH="cdt3_finish";
	
	private Vibrator mVibrator;
	private IBinder mBinder;
	private MediaPlayer mPlayer;
	private CountDownTimer cDT1, cDT2, cDT3;
	private long curTime1, curTime2, curTime3;
	private boolean ready1 = false, ready2 = false, ready3 = false;
	private boolean selected1 = false, selected2 = false, selected3 = false;
	private boolean recording = false;
	private String filePath1 = "", filePath2 = "", filePath3 = "";
	private int recipeID = -1, lastRecipeID1 = -1, lastRecipeID2 = -1, lastRecipeID3 = -1;
	private String curPath = "";
	
    @Override
    public void onCreate() {
    	super.onCreate();
		mBinder= new CountDownServiceBinder(CountDownService.this);	
		
		mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

		mPlayer = new MediaPlayer();
		mPlayer.setLooping(false);
		mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
		mPlayer.setVolume(0.5f, 0.5f);
    }

    @Override
    public void onStart(Intent intent, int startId) {
    }
    @Override
    public boolean onUnbind(Intent intent) {
		//Log.i("bind", numberBind+"");
    	return super.onUnbind(intent);
    }
	@Override
	public IBinder onBind(Intent arg0) {
//		Log.i("onBind.", "QQQQQQ");
		return mBinder;
	}

	public void updateCDT1(long time){
		if(null != cDT1) cDT1.cancel();
		cDT1 = new CountDownTimer(time, 500) {
			
			@Override
			public void onTick(long millisUntilFinished) {
				curTime1 = millisUntilFinished;
				sendBroadcast(new Intent(ACTION_CDT1_TICK));
			}
			
			@Override
			public void onFinish() {
				curTime1 = 0;
				sendBroadcast(new Intent(ACTION_CDT1_FINISH));
			}
		};
		
		cDT1.start();
	}
	
	public void updateCDT2(long time){
		if(null != cDT2) cDT2.cancel();
		cDT2 = new CountDownTimer(time, 500) {
			
			@Override
			public void onTick(long millisUntilFinished) {
				curTime2 = millisUntilFinished;
				sendBroadcast(new Intent(ACTION_CDT2_TICK));
			}
			
			@Override
			public void onFinish() {
				curTime2 = 0;
				sendBroadcast(new Intent(ACTION_CDT2_FINISH));
			}
		};
		
		cDT2.start();
	}
	
	public void updateCDT3(long time){
		if(null != cDT3) cDT3.cancel();
		cDT3 = new CountDownTimer(time, 500) {
			
			@Override
			public void onTick(long millisUntilFinished) {
				curTime3 = millisUntilFinished;
				sendBroadcast(new Intent(ACTION_CDT3_TICK));
			}
			
			@Override
			public void onFinish() {
				curTime3 = 0;
				sendBroadcast(new Intent(ACTION_CDT3_FINISH));
			}
		};
		
		cDT3.start();
	}

	public boolean isPlaying(){
		return mPlayer.isPlaying();
	}
	
	/**
	 * @return the curTime1
	 */
	public long getCurTime1() {
		return curTime1;
	}

	/**
	 * @param curTime1 the curTime1 to set
	 */
	public void setCurTime1(long curTime1) {
		this.curTime1 = curTime1;
	}

	/**
	 * @return the curTime2
	 */
	public long getCurTime2() {
		return curTime2;
	}

	/**
	 * @param curTime2 the curTime2 to set
	 */
	public void setCurTime2(long curTime2) {
		this.curTime2 = curTime2;
	}

	/**
	 * @return the curTime3
	 */
	public long getCurTime3() {
		return curTime3;
	}

	/**
	 * @param curTime3 the curTime3 to set
	 */
	public void setCurTime3(long curTime3) {
		this.curTime3 = curTime3;
	}

	/**
	 * @return the recipeID1
	 */
	public int getRecipeID() {
		return recipeID;
	}

	/**
	 * @param recipeID1 the recipeID1 to set
	 */
	public void setRecipeID(int recipeID) {
		this.recipeID = recipeID;
	}

	
	/**
	 * @return the lastRecipeID1
	 */
	public int getLastRecipeID1() {
		return lastRecipeID1;
	}

	/**
	 * @param lastRecipeID1 the lastRecipeID1 to set
	 */
	public void setLastRecipeID1(int lastRecipeID1) {
		this.lastRecipeID1 = lastRecipeID1;
	}

	/**
	 * @return the lastRecipeID2
	 */
	public int getLastRecipeID2() {
		return lastRecipeID2;
	}

	/**
	 * @param lastRecipeID2 the lastRecipeID2 to set
	 */
	public void setLastRecipeID2(int lastRecipeID2) {
		this.lastRecipeID2 = lastRecipeID2;
	}

	/**
	 * @return the lastRecipeID3
	 */
	public int getLastRecipeID3() {
		return lastRecipeID3;
	}

	/**
	 * @param lastRecipeID3 the lastRecipeID3 to set
	 */
	public void setLastRecipeID3(int lastRecipeID3) {
		this.lastRecipeID3 = lastRecipeID3;
	}

	public void playRecord(final int pos){
		
		if (pos == 1) {
			curPath = filePath1;
		} else if(pos == 2){
			curPath = filePath2;
		} else {
			curPath = filePath3;
		}
		
		mPlayer.reset();
		try {
			mPlayer.setDataSource(curPath);
			mPlayer.prepareAsync();
		} catch (Exception e) {}
		
		mPlayer.setOnPreparedListener(new OnPreparedListener() {
			
			@Override
			public void onPrepared(MediaPlayer mp) {
				mVibrator.vibrate(new long[]{0, 300, 200, 300}, -1);
				mPlayer.start();
			}
		});
		
		mPlayer.setOnCompletionListener(new OnCompletionListener() {
			
			public void onCompletion(MediaPlayer mp) {
				if (pos == 1) {
					selected1 = false;
				} else if(pos == 2){
					selected2 = false;
				} else {
					selected3 = false;
				}
				
				try {
					new File(curPath).delete();
					
				} catch (Exception e) {
					// TODO: handle exception
				}
				
				checkReadyToPlay();
			}
		});
	}

	public void checkReadyToPlay(){
		if (ready1) {
			playRecord(1);
			ready1 = false;
		}else if (ready2) {
			playRecord(2);
			ready2 = false;
		}else if (ready3) {
			playRecord(3);
			ready3 = false;
		}
	}

	
	
	/**
	 * @return the recording
	 */
	public boolean isRecording() {
		return recording;
	}

	/**
	 * @param recording the recording to set
	 */
	public void setRecording(boolean recording) {
		this.recording = recording;
	}

	/**
	 * @return the ready1
	 */
	public boolean isReady1() {
		return ready1;
	}

	/**
	 * @param ready1 the ready1 to set
	 */
	public void setReady1(boolean ready1) {
		this.ready1 = ready1;
	}

	/**
	 * @return the ready2
	 */
	public boolean isReady2() {
		return ready2;
	}

	/**
	 * @param ready2 the ready2 to set
	 */
	public void setReady2(boolean ready2) {
		this.ready2 = ready2;
	}

	/**
	 * @return the ready3
	 */
	public boolean isReady3() {
		return ready3;
	}

	/**
	 * @param ready3 the ready3 to set
	 */
	public void setReady3(boolean ready3) {
		this.ready3 = ready3;
	}

	/**
	 * @return the selected1
	 */
	public boolean isSelected1() {
		return selected1;
	}

	/**
	 * @return the selected2
	 */
	public boolean isSelected2() {
		return selected2;
	}

	/**
	 * @return the selected3
	 */
	public boolean isSelected3() {
		return selected3;
	}

	/**
	 * @param filePath1 the filePath1 to set
	 */
	public void setFilePath1(String filePath1) {
		this.filePath1 = filePath1;
	}

	/**
	 * @param filePath2 the filePath2 to set
	 */
	public void setFilePath2(String filePath2) {
		this.filePath2 = filePath2;
	}

	/**
	 * @param filePath3 the filePath3 to set
	 */
	public void setFilePath3(String filePath3) {
		this.filePath3 = filePath3;
	}

	/**
	 * @param selected1 the selected1 to set
	 */
	public void setSelected1(boolean selected1) {
		this.selected1 = selected1;
	}

	/**
	 * @param selected2 the selected2 to set
	 */
	public void setSelected2(boolean selected2) {
		this.selected2 = selected2;
	}

	/**
	 * @param selected3 the selected3 to set
	 */
	public void setSelected3(boolean selected3) {
		this.selected3 = selected3;
	}
	
	
	
}
